package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.*;
import redis.clients.jedis.util.KeyValue;

import java.util.*;

/**
 * Redis 操作工具类：SortedSet
 * 参考 {@link redis.clients.jedis.commands.SortedSetCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisSortedSetUtils {
    private RedisSortedSetUtils() {
        throw new AssertionError("不能实例化 RedisSortedSetUtils 工具类");
    }

    public static long zadd(String key, double score, String member) {
        long zadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zadd = jedis.zadd(key, score, member);
        }
        return zadd;
    }

    public static long zadd(String key, double score, String member, ZAddParams params) {
        long zadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zadd = jedis.zadd(key, score, member, params);
        }
        return zadd;
    }

    public static long zadd(String key, Map<String, Double> scoreMembers) {
        long zadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zadd = jedis.zadd(key, scoreMembers);
        }
        return zadd;
    }

    public static long zadd(String key, Map<String, Double> scoreMembers, ZAddParams params) {
        long zadd;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zadd = jedis.zadd(key, scoreMembers, params);
        }
        return zadd;
    }

    public static Double zaddIncr(String key, double score, String member, ZAddParams params) {
        Double zaddIncr = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zaddIncr = jedis.zaddIncr(key, score, member, params);
        }
        return zaddIncr;
    }

    public static long zrem(String key, String... members) {
        long zrem;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrem = jedis.zrem(key, members);
        }
        return zrem;
    }

    public static long zrem(String key, List<String> members) {
        long zrem;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrem = jedis.zrem(key, members.toArray(new String[members.size()]));
        }
        return zrem;
    }

    public static double zincrby(String key, double increment, String member) {
        double zincrby;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zincrby = jedis.zincrby(key, increment, member);
        }
        return zincrby;
    }

    public static Double zincrby(String key, double increment, String member, ZIncrByParams params) {
        double zincrby;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zincrby = jedis.zincrby(key, increment, member, params);
        }
        return zincrby;
    }

    public static Long zrank(String key, String member) {
        Long zrank = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrank = jedis.zrank(key, member);
        }
        return zrank;
    }

    public static Long zrevrank(String key, String member) {
        Long zrevrank = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrank = jedis.zrevrank(key, member);
        }
        return zrevrank;
    }

    public static List<String> zrange(String key, long start, long stop) {
        List<String> zrange = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrange = jedis.zrange(key, start, stop);
        }
        return zrange;
    }

    public static List<String> zrevrange(String key, long start, long stop) {
        List<String> zrevrange = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrange = jedis.zrevrange(key, start, stop);
        }
        return zrevrange;
    }

    public static List<Tuple> zrangeWithScores(String key, long start, long stop) {
        List<Tuple> zrangeWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeWithScores = jedis.zrangeWithScores(key, start, stop);
        }
        return zrangeWithScores;
    }

    public static List<Tuple> zrevrangeWithScores(String key, long start, long stop) {
        List<Tuple> zrevrangeWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeWithScores = jedis.zrevrangeWithScores(key, start, stop);
        }
        return zrevrangeWithScores;
    }

    public static List<String> zrange(String key, ZRangeParams zRangeParams) {
        List<String> zrange = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrange = jedis.zrange(key, zRangeParams);
        }
        return zrange;
    }

    public static List<Tuple> zrangeWithScores(String key, ZRangeParams zRangeParams) {
        List<Tuple> zrangeWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeWithScores = jedis.zrangeWithScores(key, zRangeParams);
        }
        return zrangeWithScores;
    }

    public static long zrangestore(String dest, String src, ZRangeParams zRangeParams) {
        long zrangestore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangestore = jedis.zrangestore(dest, src, zRangeParams);
        }
        return zrangestore;
    }

    public static String zrandmember(String key) {
        String zrandmember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrandmember = jedis.zrandmember(key);
        }
        return zrandmember;
    }

    public static List<String> zrandmember(String key, long count) {
        List<String> zrandmember = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrandmember = jedis.zrandmember(key, count);
        }
        return zrandmember;
    }

    public static List<Tuple> zrandmemberWithScores(String key, long count) {
        List<Tuple> zrandmemberWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrandmemberWithScores = jedis.zrandmemberWithScores(key, count);
        }
        return zrandmemberWithScores;
    }

    public static long zcard(String key) {
        long zcard;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zcard = jedis.zcard(key);
        }
        return zcard;
    }

    public static Double zscore(String key, String member) {
        Double zscore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zscore = jedis.zscore(key, member);
        }
        return zscore;
    }

    public static List<Double> zmscore(String key, String... members) {
        List<Double> zmscore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zmscore = jedis.zmscore(key, members);
        }
        return zmscore;
    }

    public static List<Double> zmscore(String key, List<String> members) {
        List<Double> zmscore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zmscore = jedis.zmscore(key, members.toArray(new String[members.size()]));
        }
        return zmscore;
    }

    public static Tuple zpopmax(String key) {
        Tuple zpopmax = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zpopmax = jedis.zpopmax(key);
        }
        return zpopmax;
    }

    public static List<Tuple> zpopmax(String key, int count) {
        List<Tuple> zpopmax = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zpopmax = jedis.zpopmax(key, count);
        }
        return zpopmax;
    }

    public static Tuple zpopmin(String key) {
        Tuple zpopmin = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zpopmin = jedis.zpopmin(key);
        }
        return zpopmin;
    }

    public static List<Tuple> zpopmin(String key, int count) {
        List<Tuple> zpopmin = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zpopmin = jedis.zpopmin(key, count);
        }
        return zpopmin;
    }

    public static long zcount(String key, double min, double max) {
        long zcount;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zcount = jedis.zcount(key, min, max);
        }
        return zcount;
    }

    public static long zcount(String key, String min, String max) {
        long zcount;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zcount = jedis.zcount(key, min, max);
        }
        return zcount;
    }

    public static List<String> zrangeByScore(String key, double min, double max) {
        List<String> zrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScore = jedis.zrangeByScore(key, min, max);
        }
        return zrangeByScore;
    }

    public static List<String> zrangeByScore(String key, String min, String max) {
        List<String> zrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScore = jedis.zrangeByScore(key, min, max);
        }
        return zrangeByScore;
    }

    public static List<String> zrevrangeByScore(String key, double max, double min) {
        List<String> zrevrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScore = jedis.zrevrangeByScore(key, max, min);
        }
        return zrevrangeByScore;
    }

    public static List<String> zrangeByScore(String key, double min, double max, int offset, int count) {
        List<String> zrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScore = jedis.zrangeByScore(key, min, max, offset, count);
        }
        return zrangeByScore;
    }

    public static List<String> zrevrangeByScore(String key, String max, String min) {
        List<String> zrevrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScore = jedis.zrevrangeByScore(key, max, min);
        }
        return zrevrangeByScore;
    }

    public static List<String> zrangeByScore(String key, String min, String max, int offset, int count) {
        List<String> zrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScore = jedis.zrangeByScore(key, min, max, offset, count);
        }
        return zrangeByScore;
    }

    public static List<String> zrevrangeByScore(String key, double max, double min, int offset, int count) {
        List<String> zrevrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScore = jedis.zrevrangeByScore(key, max, min, offset, count);
        }
        return zrevrangeByScore;
    }

    public static List<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
        List<Tuple> zrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScoreWithScores = jedis.zrangeByScoreWithScores(key, min, max);
        }
        return zrangeByScoreWithScores;
    }

    public static List<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) {
        List<Tuple> zrevrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores(key, max, min);
        }
        return zrevrangeByScoreWithScores;
    }

    public static List<Tuple> zrangeByScoreWithScores(String key, double min, double max, int offset, int count) {
        List<Tuple> zrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScoreWithScores = jedis.zrangeByScoreWithScores(key, min, max, offset, count);
        }
        return zrangeByScoreWithScores;
    }

    public static List<String> zrevrangeByScore(String key, String max, String min, int offset, int count) {
        List<String> zrevrangeByScore = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScore = jedis.zrevrangeByScore(key, max, min, offset, count);
        }
        return zrevrangeByScore;
    }

    public static List<Tuple> zrangeByScoreWithScores(String key, String min, String max) {
        List<Tuple> zrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScoreWithScores = jedis.zrangeByScoreWithScores(key, min, max);
        }
        return zrangeByScoreWithScores;
    }

    public static List<Tuple> zrevrangeByScoreWithScores(String key, String max, String min) {
        List<Tuple> zrevrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores(key, max, min);
        }
        return zrevrangeByScoreWithScores;
    }

    public static List<Tuple> zrangeByScoreWithScores(String key, String min, String max, int offset, int count) {
        List<Tuple> zrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByScoreWithScores = jedis.zrangeByScoreWithScores(key, min, max, offset, count);
        }
        return zrangeByScoreWithScores;
    }

    public static List<Tuple> zrevrangeByScoreWithScores(String key, double max, double min, int offset, int count) {
        List<Tuple> zrevrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores(key, max, min, offset, count);
        }
        return zrevrangeByScoreWithScores;
    }

    public static List<Tuple> zrevrangeByScoreWithScores(String key, String max, String min, int offset, int count) {
        List<Tuple> zrevrangeByScoreWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByScoreWithScores = jedis.zrevrangeByScoreWithScores(key, max, min, offset, count);
        }
        return zrevrangeByScoreWithScores;
    }

    public static long zremrangeByRank(String key, long start, long stop) {
        long zremrangeByRank;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zremrangeByRank = jedis.zremrangeByRank(key, start, stop);
        }
        return zremrangeByRank;
    }

    public static long zremrangeByScore(String key, double min, double max) {
        long zremrangeByScore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zremrangeByScore = jedis.zremrangeByScore(key, min, max);
        }
        return zremrangeByScore;
    }

    public static long zremrangeByScore(String key, String min, String max) {
        long zremrangeByScore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zremrangeByScore = jedis.zremrangeByScore(key, min, max);
        }
        return zremrangeByScore;
    }

    public static long zlexcount(String key, String min, String max) {
        long zlexcount;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zlexcount = jedis.zlexcount(key, min, max);
        }
        return zlexcount;
    }

    public static List<String> zrangeByLex(String key, String min, String max) {
        List<String> zrangeByLex = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByLex = jedis.zrangeByLex(key, min, max);
        }
        return zrangeByLex;
    }

    public static List<String> zrangeByLex(String key, String min, String max, int offset, int count) {
        List<String> zrangeByLex = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrangeByLex = jedis.zrangeByLex(key, min, max, offset, count);
        }
        return zrangeByLex;
    }

    public static List<String> zrevrangeByLex(String key, String max, String min) {
        List<String> zrevrangeByLex = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByLex = jedis.zrevrangeByLex(key, max, min);
        }
        return zrevrangeByLex;
    }

    public static List<String> zrevrangeByLex(String key, String max, String min, int offset, int count) {
        List<String> zrevrangeByLex = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zrevrangeByLex = jedis.zrevrangeByLex(key, max, min, offset, count);
        }
        return zrevrangeByLex;
    }

    public static long zremrangeByLex(String key, String min, String max) {
        long zremrangeByLex;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zremrangeByLex = jedis.zremrangeByLex(key, min, max);
        }
        return zremrangeByLex;
    }

    public static ScanResult<Tuple> zscan(String key, String cursor) {
        ScanResult<Tuple> zscan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zscan = jedis.zscan(key, cursor, new ScanParams());
        }
        return zscan;
    }

    public static ScanResult<Tuple> zscan(String key, String cursor, ScanParams params) {
        ScanResult<Tuple> zscan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zscan = jedis.zscan(key, cursor, params);
        }
        return zscan;
    }

    public static KeyValue<String, Tuple> bzpopmax(double timeout, String... keys) {
        KeyValue<String, Tuple> bzpopmax = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            bzpopmax = jedis.bzpopmax(timeout, keys);
        }
        return bzpopmax;
    }

    public static KeyValue<String, Tuple> bzpopmax(double timeout, List<String> keys) {
        KeyValue<String, Tuple> bzpopmax = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            bzpopmax = jedis.bzpopmax(timeout, keys.toArray(new String[keys.size()]));
        }
        return bzpopmax;
    }

    public static KeyValue<String, Tuple> bzpopmin(double timeout, String... keys) {
        KeyValue<String, Tuple> bzpopmin = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            bzpopmin = jedis.bzpopmin(timeout, keys);
        }
        return bzpopmin;
    }

    public static KeyValue<String, Tuple> bzpopmin(double timeout, List<String> keys) {
        KeyValue<String, Tuple> bzpopmin = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            bzpopmin = jedis.bzpopmin(timeout, keys.toArray(new String[keys.size()]));
        }
        return bzpopmin;
    }

    public static List<String> zdiff(String... keys) {
        List<String> zdiff = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zdiff = jedis.zdiff(keys);
        }
        return zdiff;
    }

    public static List<String> zdiff(List<String> keys) {
        List<String> zdiff = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zdiff = jedis.zdiff(keys.toArray(new String[keys.size()]));
        }
        return zdiff;
    }

    public static List<Tuple> zdiffWithScores(String... keys) {
        List<Tuple> zdiffWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zdiffWithScores = jedis.zdiffWithScores(keys);
        }
        return zdiffWithScores;
    }

    public static List<Tuple> zdiffWithScores(List<String> keys) {
        List<Tuple> zdiffWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zdiffWithScores = jedis.zdiffWithScores(keys.toArray(new String[keys.size()]));
        }
        return zdiffWithScores;
    }

    public static long zdiffStore(String dstkey, String... keys) {
        long zdiffStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zdiffStore = jedis.zdiffStore(dstkey, keys);
        }
        return zdiffStore;
    }

    public static long zdiffStore(String dstkey, List<String> keys) {
        long zdiffStore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zdiffStore = jedis.zdiffStore(dstkey, keys.toArray(new String[keys.size()]));
        }
        return zdiffStore;
    }

    public static long zinterstore(String dstkey, String... sets) {
        long zinterstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinterstore = jedis.zinterstore(dstkey, sets);
        }
        return zinterstore;
    }

    public static long zinterstore(String dstkey, List<String> sets) {
        long zinterstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinterstore = jedis.zinterstore(dstkey, sets.toArray(new String[sets.size()]));
        }
        return zinterstore;
    }

    public static long zinterstore(String dstkey, ZParams params, String... sets) {
        long zinterstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinterstore = jedis.zinterstore(dstkey, params, sets);
        }
        return zinterstore;
    }

    public static long zinterstore(String dstkey, ZParams params, List<String> sets) {
        long zinterstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinterstore = jedis.zinterstore(dstkey, params, sets.toArray(new String[sets.size()]));
        }
        return zinterstore;
    }

    public static List<String> zinter(ZParams params, String... keys) {
        List<String> zinter = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinter = jedis.zinter(params, keys);
        }
        return zinter;
    }

    public static List<String> zinter(ZParams params, List<String> keys) {
        List<String> zinter = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinter = jedis.zinter(params, keys.toArray(new String[keys.size()]));
        }
        return zinter;
    }

    public static List<Tuple> zinterWithScores(ZParams params, String... keys) {
        List<Tuple> zinterWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinterWithScores = jedis.zinterWithScores(params, keys);
        }
        return zinterWithScores;
    }

    public static List<Tuple> zinterWithScores(ZParams params, List<String> keys) {
        List<Tuple> zinterWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zinterWithScores = jedis.zinterWithScores(params, keys.toArray(new String[keys.size()]));
        }
        return zinterWithScores;
    }

    public static List<String> zunion(ZParams params, String... keys) {
        List<String> zunion = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunion = jedis.zunion(params, keys);
        }
        return zunion;
    }

    public static List<String> zunion(ZParams params, List<String> keys) {
        List<String> zunion = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunion = jedis.zunion(params, keys.toArray(new String[keys.size()]));
        }
        return zunion;
    }

    public static List<Tuple> zunionWithScores(ZParams params, String... keys) {
        List<Tuple> zunionWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunionWithScores = jedis.zunionWithScores(params, keys);
        }
        return zunionWithScores;
    }

    public static List<Tuple> zunionWithScores(ZParams params, List<String> keys) {
        List<Tuple> zunionWithScores = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunionWithScores = jedis.zunionWithScores(params, keys.toArray(new String[keys.size()]));
        }
        return zunionWithScores;
    }

    public static long zunionstore(String dstkey, String... sets) {
        long zunionstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunionstore = jedis.zunionstore(dstkey, sets);
        }
        return zunionstore;
    }

    public static long zunionstore(String dstkey, List<String> sets) {
        long zunionstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunionstore = jedis.zunionstore(dstkey, sets.toArray(new String[sets.size()]));
        }
        return zunionstore;
    }

    public static long zunionstore(String dstkey, ZParams params, String... sets) {
        long zunionstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunionstore = jedis.zunionstore(dstkey, params, sets);
        }
        return zunionstore;
    }

    public static long zunionstore(String dstkey, ZParams params, List<String> sets) {
        long zunionstore;
        try (Jedis jedis = SysJedisPool.getResource()) {
            zunionstore = jedis.zunionstore(dstkey, params, sets.toArray(new String[sets.size()]));
        }
        return zunionstore;
    }

    /*public static void main(String[] args) {
        System.out.println("zadd：" + zadd("key:zset", 0.5, "name"));
        System.out.println("zadd：" + zadd("key:zset", 0.8, "date"));
        System.out.println("zadd：" + zadd("key:zset", new HashMap<>() {{
            put("type", 0.3D);
            put("version", 0.6D);
        }}));
        System.out.println("zcard：" + zcard("key:zset"));
        System.out.println("zrem：" + zrem("key:zset", "type"));
        System.out.println("zcard：" + zcard("key:zset"));
    }*/
}
